﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;

namespace Model.RolePopedom
{
    /// <summary>
    /// 模块与权限的处理类
    /// </summary>
    public class ModulePermmissionDispose
    {
        public List<MModulePermmission> getList()
        {
            List<MModulePermmission> mplist = new List<MModulePermmission>();
            Hashtable ht = getPermissionName();

            ModulePermission nmodel = new ModulePermission();
            FieldInfo[] fieldlist = typeof(ModulePermission).GetFields();
            foreach (FieldInfo model in fieldlist)
            {
                Category[] cate = model.GetCustomAttributes(typeof(Category), false) as Category[];
                Description[] desc = model.GetCustomAttributes(typeof(Description), false) as Description[];
                PermissionOption[] option = model.GetCustomAttributes(typeof(PermissionOption), false) as PermissionOption[];

                MModulePermmission mpmodel = new MModulePermmission();
                mpmodel.ID = Convert.ToInt32(model.GetValue(nmodel));
                mpmodel.Name = cate[0].CategoryName;
                foreach (string m in option[0].PermissionOptionName.ToString().Split(','))
                {
                    MPermmission pmodel = new MPermmission();
                    pmodel.ID = Convert.ToInt32(Enum.Parse(typeof(PermissionEnum), m.Trim(), true));
                    pmodel.Name = ht[m.Trim()].ToString().Trim();
                    mpmodel.PermmissionList.Add(pmodel);
                }
                mplist.Add(mpmodel);
            }

            return mplist;
        }

        /// <summary>
        /// 获取权限枚举名称集合
        /// </summary>
        /// <returns></returns>
        private Hashtable getPermissionName()
        {
            Hashtable ht = new Hashtable();

            Type t = typeof(PermissionEnum);
            FieldInfo[] info = t.GetFields();
            for (int i = 0; i < info.Length; i++)
            {
                Description[] desc = info[i].GetCustomAttributes(typeof(Description),false) as Description[];
                if (desc.Length > 0)
                {
                    ht[info[i].Name.Trim()] = desc[0].DescriptionName;
                }
            }

            return ht;
        }
    }

    /// <summary>
    /// 模块与权限表
    /// </summary>
    public class MModulePermmission
    {
        private int _id;
        private string _name;
        private List<MPermmission> _permmissionlist = new List<MPermmission>();

        /// <summary>
        /// 模块ID
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }

        /// <summary>
        /// 模块名称
        /// </summary>
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }

        /// <summary>
        /// 权限集合
        /// </summary>
        public List<MPermmission> PermmissionList
        {
            set { _permmissionlist = value; }
            get { return _permmissionlist; }
        }
    }

    /// <summary>
    /// 权限实体类
    /// </summary>
    public class MPermmission
    {
        private long _id;
        private string _name;
        private bool _checked=false;

        /// <summary>
        /// 权限ID
        /// </summary>
        public long ID
        {
            set { _id = value; }
            get { return _id; }
        }

        /// <summary>
        /// 权限名称
        /// </summary>
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }

        /// <summary>
        /// 选中
        /// </summary>
        public bool Checked
        {
            set { _checked = value; }
            get { return _checked; }
        }
    }
}
